package action;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import listener.ServerListener;
import server.Server;
import server.imp.ChatServerImpl;
import server.imp.RegisterServerImpl;
import server.imp.ServerContainer;
import server.imp.WebServerImpl;

public  class ServerAction {
	
	public final static String CHAT_SERVER ="chat";
	public final static String WEB_SERVER ="web";
	
	private Map<String, Server> servers;
	
	public ServerAction(){
		servers = new ConcurrentHashMap<>();
		Server chatServer = new ChatServerImpl();
		Server registerServer = new RegisterServerImpl();
		ServerContainer container = new ServerContainer();
		container.add(chatServer);
		container.add(registerServer);
		servers.put(CHAT_SERVER, container);
		WebServerImpl webServer = new WebServerImpl();
		ServerListener serverListener = new ServerListener(container);
		webServer.listen(serverListener);
		servers.put(WEB_SERVER, webServer);
	}
	
	public void start(){
		for(String name:servers.keySet()){
			servers.get(name).start();
		}
	}
	
	public void start(String name){
		if (servers.containsKey(name)) {
			servers.get(name).start();
		}
	}
	
	public void shutdown(){
		for(String name:servers.keySet()){
			servers.get(name).shutdown();
		}
	}
	
	public void shutdown(String name){
		if (servers.containsKey(name)) {
			servers.get(name).shutdown();
		}
	}
	
	public static void main(String[] args) {
		new ServerAction().start();
	}
}
